package com.zjut.azhen.DBDesign.controllers.admin;

import com.zjut.azhen.DBDesign.bean.RespBean;
import com.zjut.azhen.DBDesign.bean.Students;
import com.zjut.azhen.DBDesign.bean.User;
import com.zjut.azhen.DBDesign.service.CourseService;
import com.zjut.azhen.DBDesign.service.StudentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;


@RestController
@RequestMapping("/stu")
public class StudentsController {
    @Autowired
    StudentsService studentsService;
    @Autowired
    CourseService courseService;
    @GetMapping("/")
    public RespBean getAll(){
        return RespBean.ok("success",studentsService.getAll());
    }

    @PutMapping("/")

    public RespBean updateStudent(@RequestBody Students students){
        if(studentsService.updateStuById(students)==1){
            return RespBean.ok("success",studentsService.updateStuById(students));
        }
        return RespBean.error("fail update");
    }

    @PostMapping("/")
    public RespBean intertStudent(@RequestBody Students students){
        if(studentsService.insertStu(students)==1) {
            return RespBean.ok("success");
        }
        return RespBean.error("insert failed");
    }
    @DeleteMapping("/{id}")
    public RespBean deleteStudentById(@PathVariable String id){
        if(studentsService.deleteStudentById(id)==1) {
            return RespBean.ok("success");
        }
        return RespBean.error("insert failed");
    }
    @GetMapping("/class/{term}")
    public RespBean getClassC(@PathVariable String term , HttpSession session){
            //未选课程
        //Role role=(Role) session.getAttribute("role");
        User user=(User) session.getAttribute("user");



//        if(user==null||"R03".equals(role.getRno())){
//            return RespBean.error("无效访问");
//        }
        //System.out.println(term);
        if("null".equals(term)){
            //System.out.println(term);
            return RespBean.error("空值错误");
        }

        String clno=studentsService.getStuById(user.getId()).getClno();
        //System.out.println(clno);
        //check for term
        String sno=user.getId();
        return RespBean.ok("success",courseService.getClassC(clno,term,sno));
    }

}
